package com.foxcai.novel.html;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.foxcai.novel.format.PubFormat;

/**
 * 页面方法实体类
 * 
 */
public class JspContextServices {
	/**
	 * 日志分类标识
	 */
	public static final String LOG_ID = "FcapHTML";

	/**
	 * URL的分隔符
	 */
	private static final String URL_SEPERATOR = "/";

	/**
	 * 系统错误的错误代码
	 */
	public static final String DEFAULT_ERROR_CODE = "EBLN0000";

	/**
	 * 当前web应用的Context
	 */
	private String itsWebContext;

	/**
	 * 当前请求
	 */
	private HttpServletRequest itsRequest;

	/**
	 * 初始化
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @throws TranFailException
	 */
	public void initialize(HttpServletRequest request,
			HttpServletResponse response) {
		itsRequest = request;
		this.itsWebContext = request.getContextPath() + URL_SEPERATOR;
	}

	/**
	 * 取得格式化数字<br>
	 * 示例：getFormatNumber("0.21","#.##%") = "21%"<br>
	 * 
	 * @param number
	 *            格式化前的数字
	 * @param format
	 *            格式
	 * @return String
	 */
	public String getFormatNumber(String number, String format) {
		NumberFormat formatter = new DecimalFormat(format);
		return formatter.format(new BigDecimal(number).floatValue());
	}

	/**
	 * 取得格式化的服务器时间
	 * 
	 * @param timeFormat
	 *            时间：yyyyMMddHHmmss；日期：yyyyMMdd
	 * @return String
	 */
	public static String getServerTime(String timeFormat) {
		String format = (timeFormat == null) ? PubFormat.DEFAULT_TIME_FORMAT_DB
				: timeFormat;
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * Method getWebAppPath.
	 * 
	 * @return String
	 */
	public String getWebAppPath() {
		return itsWebContext;
	}

	/**
	 * 如果是空值，则以空字符串取代
	 * 
	 * @param dataValue
	 * @return String
	 */
	public String nvl(Object dataValue) {
		return nvl(dataValue, "");
	}

	/**
	 * 如果是空值，则以参数指定字符串取代
	 * 
	 * @param dataValue
	 * @param returnValue
	 * @return String
	 */
	public String nvl(Object dataValue, String returnValue) {
		return PubFormat.nvl(dataValue, returnValue);
	}

	/**
	 * 格式化金额
	 * 
	 * @param currency
	 *            String
	 * @return String
	 */
	public String getFmtCurrency(String currency) {
		return PubFormat.formatCurrency(currency);
	}

	/**
	 * 使用默认格式格式化日期
	 * 
	 * @param date
	 * @return String
	 */
	public String getFmtDate(String date) {
		return PubFormat.formatDate(date, PubFormat.DEFAULT_DATE_FORMAT_EN);
	}

	/**
	 * 根据参数格式显示格式化日期
	 * 
	 * @param date
	 * @param format
	 *            String
	 * @return String
	 */
	public String getFmtDate(String date, String format) {
		return PubFormat.formatDate(date, format);
	}

	/**
	 * 显示中文金额
	 * 
	 * @param money
	 *            带小数点的实际金额字符
	 * @return String
	 */
	public String getChineseCurrency(String money) {
		return PubFormat.formatChineseCurrency(money);
	}

	/**
	 * 取得若干天前/后的系统日期
	 * 
	 * @param days
	 *            与现在相隔的日数，正数为当前日期之后，负数为当前日期之前
	 * @param timeFormat
	 *            格式化
	 * @return String
	 */
	public String getDifferentDate(int days, String timeFormat) {
		return PubFormat.getDifferentDate(days, timeFormat);
	}

	/**
	 * 取得指定时间间隔后的系统时间<br>
	 * 示例：<br>
	 * getDifferentTime( 1, 2, 3,"yyyyMMddHHmmss") <br>
	 * 使用yyyyMMddHHmmss格式输出1小时2分3秒后的系统时间<br>
	 * getDifferentTime( -24, 0, 0,"yyyyMMdd") <br>
	 * 使用yyyyMMdd格式输出1天前的系统日期<br>
	 * 
	 * @param hour
	 *            小时
	 * @param minute
	 *            分钟
	 * @param second
	 *            秒
	 * @param timeFormat
	 *            格式化
	 * @return String
	 */
	public String getDifferentTime(int hour, int minute, int second,
			String timeFormat) {
		return PubFormat.getDifferentTime(hour, minute, second, timeFormat);
	}

	public String getTrunPageHtm(int totle, int showNum) {
		StringBuffer pageHtm = new StringBuffer("");
		int pageNum = 1;
		String keys = "";
		int curPage;
		if (totle > showNum) {
			pageNum = totle / showNum;
			if (totle % showNum > 0) {
				pageNum = pageNum + 1;
			}
		}
		try {
			curPage = Integer.parseInt(itsRequest.getParameter("page"));
		} catch (Exception e) {
			curPage = 1;
		}
		if (itsRequest.getParameter("wd") != null
				&& !itsRequest.getParameter("wd").equals("")) {
			keys = itsRequest.getParameter("wd").trim();
		}
		int startNum = (curPage / 9) * 9;
		if (startNum == 0) {
			startNum = 1;
		}
		if (pageNum > 1 && keys.equals("")) {
			if (curPage > 1) {
				pageHtm.append("翻页：&nbsp;<a href=?page=" + (curPage - 1)
						+ ">上一页</a>&nbsp;&nbsp;|&nbsp;");
			} else {
				pageHtm.append("翻页：&nbsp;");
			}
			for (int i = startNum; i <= pageNum; i++) {
				if (i == curPage) {
					pageHtm.append("第" + i + "页&nbsp;|&nbsp;");
					continue;
				}
				pageHtm.append("<a href=?page=" + i + ">第" + i
						+ "页</a>&nbsp;|&nbsp;");
				if (i > startNum + 8) {
					// pageHtm.append("...<a href=?page="+pageNum+">第"+pageNum+"页</a>&nbsp;|&nbsp;");
					break;
				}
			}
			if (curPage < pageNum) {
				pageHtm.append("&nbsp;<a href=?page=" + (curPage + 1)
						+ ">下一页</a>&nbsp;|");
			} else {
				pageHtm.append("&nbsp;");
			}
		} else if (pageNum > 1) {
			if (curPage > 1) {
				pageHtm.append("翻页：&nbsp;<a href=?page=" + (curPage - 1)
						+ "&wd=" + keys + ">上一页</a>&nbsp;&nbsp;|&nbsp;");
			} else {
				pageHtm.append("翻页：&nbsp;");
			}
			for (int i = startNum; i <= pageNum; i++) {
				if (i == curPage) {
					pageHtm.append("第" + i + "页&nbsp;|&nbsp;");
					continue;
				}
				pageHtm.append("<a href=?page=" + i + "&wd=" + keys + ">第" + i
						+ "页</a>&nbsp;|&nbsp;");
				if (i > startNum + 8) {
					// pageHtm.append("...<a href=?page="+pageNum+"&wd="+keys+">第"+pageNum+"页</a>&nbsp;|&nbsp;");
					break;
				}
			}
			if (curPage < pageNum) {
				pageHtm.append("&nbsp;<a href=?page=" + (curPage + 1) + "&wd="
						+ keys + ">下一页</a>&nbsp;|");
			} else {
				pageHtm.append("&nbsp;");
			}
		}
		try {
			return pageHtm.substring(0, pageHtm.lastIndexOf("|"));
		} catch (Exception e) {
			return "";
		}
	}

}
